package com.credithc.signature.facade.service;

import com.alibaba.fastjson.JSONObject;
import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.signature.common.constant.FddConstant;
import com.credithc.signature.common.mapper.FddAuthCompanyMapper;
import com.credithc.signature.common.mapper.FddAuthPersonMapper;
import com.credithc.signature.common.mapper.FddSealInfoMapper;
import com.credithc.signature.common.mapper.SignCallbackMapper;
import com.credithc.signature.common.po.*;
import com.credithc.signature.common.ro.GetExtSignPageRo;
import com.credithc.signature.common.ro.facade.FddAuthReq;
import com.credithc.signature.common.ro.facade.FddSignReq;
import com.credithc.signature.common.ro.facade.QueryAuthenticationStatusReq;
import com.credithc.signature.common.ro.sign.BatchSignDataRo;
import com.credithc.signature.common.ro.sign.GetExtBatchSignPageRo;
import com.credithc.signature.common.service.ChannelService;
import com.credithc.signature.common.utils.UUIDUtil;
import com.credithc.signature.facade.client.FadadaRemoteClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc: <br/>
 * @author: Jingbo Hu<br/>
 * @time：2020年07⽉14⽇<br/>
 * @version：v1.0.0<br>
 */
@Slf4j
@Service
public class FddParamVerifyService {

    @Autowired
    MessageFactory messageFactory;

    @Value("${signature.fadada.callback.url}")
    private String callbackUrl;
    @Value("${signature.fadada.sign.callback.url}")
    private String signCallbackUrl;
    @Value("${signature.fadada.callback.client.url}")
    private String clientUrl;

    @Autowired
    ChannelService channelService;

    @Autowired
    SignCallbackMapper signCallbackMapper;
    @Autowired
    FddSealInfoMapper fddSealInfoMapper;
    @Autowired
    FddAuthCompanyMapper fddAuthCompanyMapper;
    @Autowired
    FddAuthPersonMapper fddAuthPersonMapper;


    /**
     * 校验注册、获取实名认证地址接口参数
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams(FddAuthReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getOpenId())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "openId 字段不能为空");
        }
        if (req.getAccountType() == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "accountType 字段不能为空");
        }
        if (StringUtils.isBlank(req.getVerifiedWay())) {
            // 认证方式 默认为1 三要素补充方案: 补充方案[运营商三要素/银行卡四要素通过的情况下认证成功，不通过的情况下进行人脸识别认证],按照约定只能传1
            req.setVerifiedWay("1");
        }
        if (StringUtils.isBlank(req.getMVerifiedWay())) {
            // 认证方式 默认为1 三要素补充方案
            req.setMVerifiedWay("1");
        }
        if (StringUtils.isBlank(req.getPageModify())) {
            // 是否允许用户页面修改 1:允许 2:不允许 默认不允许
            req.setPageModify("2");
        }
        if (StringUtils.isBlank(req.getReturnUrl())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "returnUrl 字段不能为空");
        }
        if (StringUtils.isBlank(req.getNotifyUrl())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "notifyUrl 字段不能为空");
        }
        if (StringUtils.isBlank(req.getCallbackUrl())) {
            req.setCallbackUrl(callbackUrl);
        }
        if (StringUtils.isBlank(req.getCertFlag())) {
            // 是否认证成功后自动申请实名证书 0:不申请, 1:自动申请
            req.setCertFlag("1");
        }
        if (req.getReturnNotifyUrlParams() == null) {
            req.setReturnNotifyUrlParams(false);
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 校验申请证书接口参数
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams2(FddAuthReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (req.getCertType() == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "certType 字段不能为空");
        }
        if (StringUtils.isBlank(req.getCustomerId())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "customerId 字段不能为空");
        }
        if (StringUtils.isBlank(req.getTransactionId())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "transactionId 字段不能为空");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 校验下载图片接口参数
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams3(FddAuthReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getUuid())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "uuid 字段不能为空");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 校验上传合同接口参数
     *
     * @param req
     * @return
     */
    public Message verifyParams4(FddAuthReq req) {
        if (StringUtils.isBlank(req.getContractId())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "contractId 字段不能为空");
        }
        if (StringUtils.isBlank(req.getDocTitle())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "docTitle 字段不能为空");
        }
        if (StringUtils.isBlank(req.getDocType())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "docType 字段不能为空");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 合同下载接口参数校验
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams5(FddAuthReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getContractId())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "contractId 字段不能为空");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 修改用户信息接口参数校验
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams6(FddAuthReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getCustomerId())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "customerId 字段不能为空");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 合同归档参数接口参数校验
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams7(FddAuthReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getContractId())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "contractId 字段不能为空");
        }
        if (req.getContractOver() == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "contractOver 字段不能为空");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 查询用户是否存在接口参数校验
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams8(FddAuthReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getCustomerIdentNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "customerIdentNo 字段不能为空");
        }
        if (StringUtils.isBlank(req.getCustomerName())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "customerName 字段不能为空");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 校验获取认证状态接口参数
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams9(QueryAuthenticationStatusReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 校验获取用户印章接口参数
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams10(FddAuthReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getCustomerId())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "customerId 字段不能为空");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 校验查询公章信息接口参数
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams11(FddAuthReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 校验查询公章信息接口参数
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams12(@Valid FddSignReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getContractId())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "contractId 字段不能为空");
        }
        if (req.getContractOver() == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "contractOver 字段不能为空");
        }
        if (req.getSealType() == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "sealType 字段不能为空");
        }
        if (req.getSealType() == 0) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "sealType 字段不能为0");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    /**
     * 查询合同状态接口参数校验
     *
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams13(FddSignReq req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getContractId())){
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "contractId 字段不能不空");
        }

        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }


    @Autowired
    FadadaRemoteClient remote;
    /**
     * 获取签章连接接口参数校验与拼接
     * @param req
     * @param encryptKey
     * @return
     */
    public Message verifyParams14(GetExtSignPageRo req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getContractId())){
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "contractId 字段不能不空");
        }
        if (StringUtils.isBlank(req.getCustomerId())&&StringUtils.isBlank(req.getSealCode())){
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "签章用户不能为空");
        }
        if (StringUtils.isNotBlank((req.getCustomerId()))){
            FddAuthPerson fddAuthPerson = fddAuthPersonMapper.selectByCustomerId(req.getCustomerId());
            if (fddAuthPerson ==null){
                return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未检索到该客户号");
            }
            if (StringUtils.isBlank(fddAuthPerson.getCustomerName())){
                QueryAuthenticationStatusReq reqS = new QueryAuthenticationStatusReq();
                reqS.setAccountType(1);
                reqS.setChannelNo(req.getChannelNo());
                reqS.setTransactionNo(fddAuthPerson.getTransactionId());
                Message message = remote.queryStatus(reqS);
                Object data = message.getData();
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(data));
                JSONObject person = jsonObject.getJSONObject("person");
                String personName = person.getString("personName");
                req.setSignKeyword("$_"+personName+"_$");
            }else {
                req.setSignKeyword("$_" + fddAuthPerson.getCustomerName() + "_$");
            }
        }
        if(StringUtils.isBlank(req.getVerifyWay())){
            req.setVerifyWay("1");
        }
        //根据公章码获取customerId
        if (StringUtils.isNotBlank(req.getSealCode())){
            Example example = new Example(FddSealInfo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("sealCode",req.getSealCode());
            FddSealInfo fddSealInfo = fddSealInfoMapper.selectOneByExample(example);
            FddAuthCompany fddAuthCompany = new FddAuthCompany();
            fddAuthCompany.setOrganization(fddSealInfo.getIdentificationNo());
            fddAuthCompany.setStatus(FddConstant.COMPANY_AUTH_SUCCESS);
            FddAuthCompany company = fddAuthCompanyMapper.selectByOrganization(fddAuthCompany);
            if (company == null) {
                log.error("根据organization字段查询fdd_auth_company表信息失败......");
                return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "根据organization字段查询fdd_auth_company表信息失败......");
            }
            req.setCustomerId(company.getCustomerId());
            req.setSignKeyword(fddSealInfo.getKeyWords());
        }
        //整理参数、保存回调
        SignCallback signCallback = new SignCallback();
        signCallback.setCallbackUrl(req.getNotifyUrl());
        signCallback.setContractId(req.getContractId());
        signCallback.setCustomerId(req.getCustomerId());
        signCallback.setSealCode(req.getSealCode());
        signCallback.setTransactionId(UUIDUtil.getUUID());
        signCallback.setStatus(3);
        signCallbackMapper.insert(signCallback);
        req.setTransactionId(signCallback.getTransactionId());
        req.setRequestSn(signCallback.getTransactionId());
        req.setClientUrl(clientUrl);
        req.setNotifyUrl(signCallbackUrl);

        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    public Message verifyParams15(GetExtSignPageRo req, String encryptKey) {
        if (StringUtils.isBlank(req.getChannelNo())) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "channelNo 字段不能为空");
        }
        String channelNo = req.getChannelNo();
        Channel channel = channelService.selectByChannelNo(channelNo);
        if (channel == null) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "未查询到此渠道,请联系管理员");
        }
        String encryptKey1 = channel.getEncryptKey();
        if (!encryptKey.equals(encryptKey1)) {
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "渠道编号和秘钥不匹配,请检查");
        }
        if (StringUtils.isBlank(req.getCustomerId())&&StringUtils.isBlank(req.getSealCode())){
            return messageFactory.getInstance(CoreCodes.CORE_SYS_ERROR, "签章用户不能为空");
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    public GetExtBatchSignPageRo bulidBatchSignUrl(GetExtSignPageRo req) {
        String batchId= UUIDUtil.getUUID();
        GetExtBatchSignPageRo getExtBatchSignPageRo = new GetExtBatchSignPageRo();
        getExtBatchSignPageRo.setBatchId(batchId);
        getExtBatchSignPageRo.setClientUrl(clientUrl);
        getExtBatchSignPageRo.setNotifyUrl(signCallbackUrl);
        getExtBatchSignPageRo.setReturnUrl(req.getReturnUrl());
        getExtBatchSignPageRo.setRequestSn(batchId);
        getExtBatchSignPageRo.setChannelNo(req.getChannelNo());
        getExtBatchSignPageRo.setBatchTitle(req.getBatchTitle());

        String keyWord = "";
        String customerId = "";
        if (StringUtils.isNotBlank((req.getCustomerId()))){
            FddAuthPerson fddAuthPerson = fddAuthPersonMapper.selectByCustomerId(req.getCustomerId());
            if (fddAuthPerson ==null){
               throw new RuntimeException("未检索到该客户号");
            }
            if (StringUtils.isBlank(fddAuthPerson.getCustomerName())){
                QueryAuthenticationStatusReq reqS = new QueryAuthenticationStatusReq();
                reqS.setAccountType(1);
                reqS.setChannelNo(req.getChannelNo());
                reqS.setTransactionNo(fddAuthPerson.getTransactionId());
                Message message = remote.queryStatus(reqS);
                Object data = message.getData();
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(data));
                JSONObject person = jsonObject.getJSONObject("person");
                String personName = person.getString("personName");
                keyWord = "$_"+personName+"_$";
            }else {
                keyWord ="$_"+fddAuthPerson.getCustomerName()+"_$";
            }
            customerId = req.getCustomerId();
        }

        if (StringUtils.isNotBlank(req.getSealCode())){
            Example example = new Example(FddSealInfo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("sealCode",req.getSealCode());
            FddSealInfo fddSealInfo = fddSealInfoMapper.selectOneByExample(example);
            FddAuthCompany fddAuthCompany = new FddAuthCompany();
            fddAuthCompany.setOrganization(fddSealInfo.getIdentificationNo());
            fddAuthCompany.setStatus(FddConstant.COMPANY_AUTH_SUCCESS);
            FddAuthCompany company = fddAuthCompanyMapper.selectByOrganization(fddAuthCompany);
            if (company == null) {
                log.error("根据organization字段查询fdd_auth_company表信息失败......");
                throw new RuntimeException("根据organization字段查询fdd_auth_company表信息失败......");
            }
            customerId = company.getCustomerId();
            keyWord = fddSealInfo.getKeyWords();
        }
        getExtBatchSignPageRo.setCustomerId(customerId);
        ArrayList<String> contractIds = req.getContractIds();
        List<BatchSignDataRo> signDatas = new ArrayList<>();
        for (String contractId: contractIds) {
            BatchSignDataRo signData1 = new BatchSignDataRo();
            signData1.setContractId(contractId);
            signData1.setKeywordStrategy("0");
            signData1.setSignKeyword(keyWord);
            signData1.setTransactionId(UUIDUtil.getUUID());
            signDatas.add(signData1);
        //保存回调
            SignCallback signCallback = new SignCallback();
            signCallback.setCallbackUrl(req.getNotifyUrl());
            signCallback.setContractId(contractId);
            signCallback.setCustomerId(req.getCustomerId());
            signCallback.setSealCode(req.getSealCode());
            signCallback.setTransactionId(signData1.getTransactionId());
            signCallback.setStatus(3);
            signCallback.setBatchId(batchId);
            signCallbackMapper.insert(signCallback);
        }
        getExtBatchSignPageRo.setSignData(signDatas);
        return getExtBatchSignPageRo;
    }
}
